package com.example.controller;

import com.example.annotations.*;
import com.example.service.CacheService;
import com.example.utils.imagecode.GetMessage;
import io.vertx.core.buffer.Buffer;
import io.vertx.core.http.HttpServerRequest;
import io.vertx.core.http.HttpServerResponse;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;


/**
 * @Classname ImageCodeController
 * @Description TODO
 * @Date 2022/7/28 11:36
 * @Created by XiongXiong
 * @Author: X.I.O
 */
@RestController(value = "/image",desc = "图片验证码")
public class ImageCodeController {


    @Auth
    @GetMapping(value = "/code",desc = "获取验证码")
    public void imageCodeController(@Param("code")String code, HttpServerResponse response, HttpServerRequest request){
        response.putHeader("Pragma", "No-cache");
        response.putHeader("Cache-Control", "no-cache");
        response.putHeader("Expires", "0");
        response.putHeader("content-type", "image/jpeg");

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        BufferedImage image= GetMessage.getImage(code);
        try {
            ImageIO.write(image, "JPEG", bos);
            byte[] imageByteDataArray = bos.toByteArray();
            Buffer buffer = Buffer.buffer();
            buffer.appendBytes(imageByteDataArray);
            response.end(buffer);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
